package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import com.ruoyi.common.core.domain.AjaxResult;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.DongContractVo;
import com.ruoyi.system.domain.bo.DongContractBo;
import com.ruoyi.system.service.IDongContractService;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 业务合同Controller
 *
 * @author ruoyi
 * @date 2022-08-10
 */
@Validated
@Api(value = "业务合同控制器", tags = {"业务合同管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/contract")
public class DongContractController extends BaseController {

    private final IDongContractService iDongContractService;

    /**
     * 查询业务合同列表
     */
    @ApiOperation("查询业务合同列表")
    @PreAuthorize("@ss.hasPermi('system:contract:list')")
    @GetMapping("/list")
    public TableDataInfo<DongContractVo> list(DongContractBo bo, PageQuery pageQuery) {
        return iDongContractService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出业务合同列表
     */
    @ApiOperation("导出业务合同列表")
    @PreAuthorize("@ss.hasPermi('system:contract:export')")
    @Log(title = "业务合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DongContractBo bo, HttpServletResponse response) {
        List<DongContractVo> list = iDongContractService.queryList(bo);
        ExcelUtil.exportExcel(list, "业务合同", DongContractVo.class, response);
    }

    /**
     * 获取业务合同详细信息
     */
    @ApiOperation("获取业务合同详细信息")
    @PreAuthorize("@ss.hasPermi('system:contract:query')")
    @GetMapping("/{id}")
    public R<DongContractVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iDongContractService.queryById(id));
    }

    /**
     * 新增业务合同
     */
    @ApiOperation("新增业务合同")
    @PreAuthorize("@ss.hasPermi('system:contract:add')")
    @Log(title = "业务合同", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult add(@Validated(AddGroup.class) @RequestBody DongContractBo bo) {
        return toAjax(iDongContractService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改业务合同
     */
    @ApiOperation("修改业务合同")
    @PreAuthorize("@ss.hasPermi('system:contract:edit')")
    @Log(title = "业务合同", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult edit(@Validated(EditGroup.class) @RequestBody DongContractBo bo) {
        return toAjax(iDongContractService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除业务合同
     */
    @ApiOperation("删除业务合同")
    @PreAuthorize("@ss.hasPermi('system:contract:remove')")
    @Log(title = "业务合同", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable String[] ids) {
        return toAjax(iDongContractService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
